package com.koalac.ironman.common.module;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Toast;

import com.foot.travel.otto.BusProvider;
import com.koalac.ironman.util.GBLActivityManager;
import com.koalac.ironman.util.HandlerUtil;
import com.koalac.ironman.util.ToastUtil;
import com.koalac.ironman.viewhelper.FinalActivity;
import com.koalac.ironman.widget.LoadingDialog;

public abstract class BaseActivity extends FinalActivity {
	// protected int width, height;
	// protected static final String JMM_GC_CACHE_IS_POOR =
	// "JMM_GC_CACHE_IS_POOR";
	protected boolean pauseOnScroll = false;
	protected boolean pauseOnFling = true;
	private int clickBackCount = 0;
	private volatile String tmpPictureName = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		GBLActivityManager.getActivityManager().pushActivity(this);
		/**
		 * 全局设为竖屏
		 */
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		HandlerUtil.clear();
	}

	protected void onClickTwiceFinished() {
		clickBackCount++;
		if (clickBackCount >= 2) {
			finishedAll();
		} else {
			Toast.makeText(this, "再按一次退出应用", 3).show();
		}
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
	}

	@Override
	protected void onResume() {
		super.onResume();
		BusProvider.getBusInstance().register(this);
//		JPushInterface.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		BusProvider.getBusInstance().unregister(this);
//		JPushInterface.onPause(this);
	}
	
	protected void postEvent(Object obj){
	    BusProvider.getBusInstance().post(obj);
	}
	
	@Override
	protected void onStop(){
		super.onStop();
	}

	/**
	 * 
	 * 
	 * @description 完成所有
	 * @Date 2013 2013-11-25
	 * @author chenzf
	 */
	protected void finishedAll() {
		GBLActivityManager.getActivityManager().popAllActivityExceptOne(null);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	/**
	 * 资源清理
	 */
	public void clear() {
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		this.clear();
	}
	public void toast(String text) {
		ToastUtil.toast(BaseActivity.this, text);
	}

	public void toast(int textID) {
		ToastUtil.toast(BaseActivity.this, textID);
	}
	/**
	 * 显示加载框
	 */
	public LoadingDialog showLoadingDialog(String text) {
		LoadingDialog loadingDialog = LoadingDialog.createLoadingDialog(
				BaseActivity.this, "", text);
		loadingDialog.show();
		return loadingDialog;

	}

	/**
	 * 显示默认加载框
	 */
	public LoadingDialog showLoadingDialog() {
		LoadingDialog loadingDialog = LoadingDialog.createLoadingDialog(
				BaseActivity.this, "", "正在加载...");
		loadingDialog.show();
		return loadingDialog;

	}
	
	public void canceledLoadingDialog(LoadingDialog loadingDialog) {
	    loadingDialog.cancel();;
	}
	
	/**
	 * DK 初次注册成功后，开发者通过在自定义的 Receiver 里监听 Action -
	 * cn.jpush.android.intent.REGISTRATION 来获取对应的 RegistrationID。
	 * 注册成功后，也可以通过此函数获取
	 */
	public static String getRegistrationID(Context context) {
	    return System.currentTimeMillis()+ "";
//		return JPushInterface.getRegistrationID(context);
	}
	/**
	 * 获取版本号
	 * 
	 * @return 当前应用的版本号
	 */
	public String getVersion() {
		try {
			PackageManager manager = this.getPackageManager();
			PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
			String version = info.versionName;
			return version;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}
	
	/**
	 * 这段代码逻辑就是计算出键盘的高度,Activity的布局整体向上移,目的是不让弹出的键盘遮住输入框
	 */
	protected void checkKeyboardHeight(final View parentLayout,final int mBottomBarHeight) {

		parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
				new ViewTreeObserver.OnGlobalLayoutListener() {

					@Override
					public void onGlobalLayout() {

						Rect r = new Rect();
						parentLayout.getWindowVisibleDisplayFrame(r);
						int screenHeight = parentLayout.getRootView()
								.getHeight();
						/**
						 * 键盘的高度
						 */
						int heightDifference = screenHeight - (r.bottom);

						if (heightDifference > 0) {
							/**
							 * 键盘已经弹出,Activity的布局整体往上移
							 */
							if (mBottomBarHeight > 0 && mBottomBarHeight < heightDifference) {
								int heightDP = heightDifference
										- mBottomBarHeight;
								parentLayout.scrollTo(0, heightDP);
							}
						} else {
							/**
							 * 键盘收起,Activity的布局还原
							 */
							if (mBottomBarHeight > 0) {
								parentLayout.scrollTo(0, 0);
							}
						}
					}
				});
	}
}
